package com.jnu.ilearn.utils;

import android.os.Handler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;


public class UserUtils {
    int insert=0;
    int change=0;
    int update=0;
    //添加用户
    public void  addUser ( Handler handler,String account, String password, String alias, String identity){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String dburl = "jdbc:mysql://120.24.40.131:3306/iLearn?&useSSL=false&serverTimezone=UTC";
                    Connection conn = DriverManager.getConnection(dburl, "root", "19_Learn");
                    String sql="insert into user (Account,Password,alias,identity,coin)values(?,?,?,?,?)";
                    PreparedStatement ptmt=conn.prepareStatement(sql);
                    ptmt.setString(1,account);
                    ptmt.setString(2,password);
                    ptmt.setString(3,alias);
                    ptmt.setString(4,identity);
                    ptmt.setInt(5,0);
                    insert = ptmt.executeUpdate();
                    DBUtils.disconnect(conn);
                    handler.sendEmptyMessage(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
    //密码更改
    public void  changePassword( Handler handler,String account,String newPassword){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String dburl = "jdbc:mysql://120.24.40.131:3306/iLearn?&useSSL=false&serverTimezone=UTC";
                    Connection conn = DriverManager.getConnection(dburl, "root", "19_Learn");
                    String sql="update user SET Password =? where Account=?";
                    PreparedStatement ptmt=conn.prepareStatement(sql);
                    ptmt.setString(1,newPassword);
                    ptmt.setString(2,account);
                    change = ptmt.executeUpdate();
                    DBUtils.disconnect(conn);
                    handler.sendEmptyMessage(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    public void  UpdatePoint( Handler handler,String account,int new_point){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    String dburl = "jdbc:mysql://120.24.40.131:3306/iLearn?&useSSL=false&serverTimezone=UTC";
                    Connection conn = DriverManager.getConnection(dburl, "root", "19_Learn");
                    String sql="update user SET coin =? where Account=?";
                    PreparedStatement ptmt=conn.prepareStatement(sql);
                    ptmt.setInt(1,new_point);
                    ptmt.setString(2,account);
                    update = ptmt.executeUpdate();
                    DBUtils.disconnect(conn);
                    handler.sendEmptyMessage(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public boolean isInsert(){
        if(insert==0){
            return false;
        }
        return true;
    }

    public boolean isChange(){
        if(change==0){
            return false;
        }
        return true;
    }
    public boolean isUpdate(){
        if(update==0){
            return false;
        }
        return true;
    }

}
